package ui.handlers;

import java.awt.BorderLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;
import ui.QuantityBox;

import javax.swing.JFrame;
import javax.swing.JLabel;

import ui.PlaceOrderScreen;
import ui.ChefHomeScreen;
import ui.ViewTransactionScreen;
import ui.ConfirmOrderScreen;
import ui.CashierHomeScreen;

import ui.FindCustomerScreen;

public class OrderHandler extends BaseHandler implements ActionListener {
	
	PlaceOrderScreen placeOrderScrn = null;
	ChefHomeScreen chefHomeScrn = null;
	ViewTransactionScreen viewTranScrn = null;
	ConfirmOrderScreen confirmOrderScrn = null;
	CashierHomeScreen cashierHomeScrn = null;
	
	
	public OrderHandler(PlaceOrderScreen pos)
	{ placeOrderScrn = pos; }
	
	public OrderHandler(ChefHomeScreen vos)
	{ chefHomeScrn = vos; }
	
	public OrderHandler(ViewTransactionScreen vts)
	{ viewTranScrn = vts; }
	
	public OrderHandler(ConfirmOrderScreen cos)
	{ confirmOrderScrn = cos; }
	
	public OrderHandler(CashierHomeScreen chs)
	{ cashierHomeScrn = chs; }
	
	public void actionPerformed(ActionEvent e)
	{
		Object src = e.getSource();
		
		if (placeOrderScrn != null)
		{
			if (src == placeOrderScrn.getSubmitButton())
			{ submitOrder(); }
			else if (src == placeOrderScrn.getCancelButton())
			{ close(placeOrderScrn.getMainFrame()); }
		}
		else if (chefHomeScrn != null)
		{
			if (src == chefHomeScrn.getCompleteButton())
			{ completeOrder(); }
		}
		else if (viewTranScrn != null)
		{
			if (src == viewTranScrn.getPendingButton())
			{ try {
				viewPendingTransactions();
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} }
			else if (src == viewTranScrn.getCompleteButton())
			{ try {
				viewCompleteTransactions();
			} catch (RemoteException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} }
		}
		else if (confirmOrderScrn != null)
		{
			if (src == confirmOrderScrn.getConfirmButton())
			{ confirmOrder(); }
			else if (src == confirmOrderScrn.getEditButton())
			{ close(confirmOrderScrn.getMainFrame()); }
		}
		else if (cashierHomeScrn != null)
		{
			if (src == cashierHomeScrn.getPlaceOrderButton())
			{ new FindCustomerScreen(); }
		}
	}
	
	private void viewPendingTransactions() throws RemoteException
	{ viewTranScrn.doMain(viewTranScrn.getStore().getPendingTransactions()); }
	
	private void viewCompleteTransactions() throws RemoteException
	{ viewTranScrn.doMain(viewTranScrn.getStore().getCompleteTransactions()); }
	
	private void completeOrder()
	{
		try {
			//TODO: change this - it should work on id of selected order, not on a new get
			//this will cause bugs if a new transaction is added to the start of submittedOrders 
			//		in between viewing the chef screen and clicking the complete button
			chefHomeScrn.getStore().markOrderComplete(chefHomeScrn.getStore().getSubmittedOrders().get(0));
			
			chefHomeScrn.getMenuContent().removeAll();
			chefHomeScrn.getMainContent().validate();
			
			chefHomeScrn.getMainContent().remove(chefHomeScrn.getMenuContent());
			
			chefHomeScrn.getMainFrame().remove(chefHomeScrn.getMainContent());
			
			chefHomeScrn.doOrder();
			
			chefHomeScrn.getMainFrame().add(chefHomeScrn.getMainContent());
			chefHomeScrn.getMainFrame().validate();
			
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (IndexOutOfBoundsException e){
			//Do nothing, catches instance where user presses button without any submitted orders
		}
	}
	
	private void addItemsToOrder() throws HeadlessException, RemoteException
	{
		for (int i : placeOrderScrn.getStore().getMenuItems())
		{
			QuantityBox qb = placeOrderScrn.getQuantityBoxes().get(i);
			boolean zeroOrEmpty = ( (qb.getText().equals("0")) || (qb.getText().equals("")) );
			if (!zeroOrEmpty)
			{
				int quantity = 0;
				try
				{ quantity = Integer.parseInt(qb.getText()); }
				catch (Exception e)
				{
					JFrame alert = new JFrame();
					alert.setTitle("ERROR");
					alert.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					alert.setSize(400, 300);
					alert.setLayout(new BorderLayout());
					
					JLabel alertText = null;
					try {
						alertText = new JLabel("Invalid quantity for "+
								placeOrderScrn.getStore().getMenuItemName(qb.getID()));
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					
					alert.add(alertText, BorderLayout.CENTER);
					return;
				}
				
				placeOrderScrn.getStore().addItemToOrder(placeOrderScrn.getOrderId(), qb.getID(), quantity);
			}
		}
	}
	
	
	private void submitOrder()
	{
		try {
			addItemsToOrder();
		} catch (HeadlessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		new ConfirmOrderScreen(placeOrderScrn.getOrderId(), this); //opens the ConfirmOrderScreen
	}
	
	private void confirmOrder()
	{
		try {
			confirmOrderScrn.getStore().submitOrder(confirmOrderScrn.getOrderId());
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		close(confirmOrderScrn.getPlaceOrderHandler().placeOrderScrn.getMainFrame());
		close(confirmOrderScrn.getMainFrame());
	}
}